﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using LNSServer.Models;
using LNSServer.ViewModels.MapModel;
using LNSServer.ViewModels.Robot;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace LNSServer.Controllers
{
    /// <summary>
    /// 地图
    /// </summary>
    [DynamicApiController]
    public class LnsMapModelController
    {
        private readonly ILogger<LnsMapModelController> logger;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public LnsMapModelController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
            this.logger = logger;
        }

        /// <summary>
        /// 地图列表
        /// </summary>
        /// <param name="dtsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> List([FromBody] MapModelSearchForm dtsf)
        {
            RefAsync<int> total = 0;
            List<MapModel> ditus = await db.Queryable<MapModel>()
                .Mapper(dt => dt.User, dt => dt.UId, dt => dt.User.Id)
                .Where(dt => dt.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(dtsf.Content), dt => dt.Name.Contains(dtsf.Content) || dt.Description.Contains(dtsf.Content))
                .WhereIF(!string.IsNullOrEmpty(dtsf.Tag), dt => dt.Tag == dtsf.Tag)
                .WhereIF(dtsf.UId != 0, dt => dt.UId == dtsf.UId)
                .ToPageListAsync(dtsf.PageIndex, dtsf.PageSize, total);

            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = ditus.Adapt<List<MapModelView>>(),
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 地图分类列表
        /// </summary>
        /// <param name="dtsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> TagList()
        {
            List<string> tags = await db.Queryable<MapModel>().Distinct().Select(rbt => rbt.Tag).ToListAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = tags,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加地图
        /// </summary>
        /// <param name="dtsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Save([FromBody] MapModelSaveForm dtsf)
        {
            await db.Insertable<MapModel>(dtsf.Adapt<MapModel>()).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = dtsf,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除地图
        /// </summary>
        /// <param name="dtddf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete([FromBody] MapModelDeleteForm dtddf) {
            await db.Updateable<MapModel>().SetColumns(mx => mx.IsActive == 0).Where(mx => mx.Id == dtddf.Id).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 修改地图
        /// </summary>
        /// <param name="dtuf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] MapModelUpdateForm dtuf) {
            await db.Updateable<MapModel>(dtuf).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = dtuf,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看地图
        /// </summary>
        /// <param name="dtsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show([FromBody] MapModelShowForm dtsf) {
            MapModel ditu = await db.Queryable<MapModel>()
                .InSingleAsync(dtsf.Id);
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = ditu,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
